import { OK_CODE } from '@/app/keys'
import { ref } from 'vue'
import { reqMsgListApi } from '@/api/leaveMsgApi'

const useMsg = (user_id, page, size) => {
  const message = ref([])
  const total = ref(0)

  const refreshLeaveMsgList = async () => {
    console.log(user_id.value)
    if (user_id.value <= 0 || page.value <= 0 || size.value === 0) return
    try {
      const { code, data } = await reqMsgListApi(user_id.value, 1, page.value, size.value)
      if (code === OK_CODE) {
        message.value = data.list || []
        total.value = data.total || 0
      }
    } catch (error) {
      console.error(error)
    }
  }

  watch([page, size, user_id], () => {
    refreshLeaveMsgList()
  }, {
    immediate: true
  })

  return {
    message,
    total,
  }
}

export default useMsg
